package com.hy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hy.domain.BlogComments;
import com.hy.domain.dto.CommentDTO;
import com.hy.service.BlogCommentsService;
import com.hy.mapper.BlogCommentsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
* @author hou
* @description 针对表【blog_comments(文章评论)】的数据库操作Service实现
* @createDate 2023-04-16 15:11:39
*/
@Service
public class BlogCommentsServiceImpl extends ServiceImpl<BlogCommentsMapper, BlogComments>
    implements BlogCommentsService{

    @Autowired
    private BlogCommentsMapper blogCommentsMapper;

    @Override
    public List<CommentDTO> listCommentsByBlogId(Long blogId) {
//        final List<BlogComments> comments = lambdaQuery().eq(BlogComments::getBlogId, blogId).list();
        // 处理一级评论和二级评论
        final List<CommentDTO> comments = blogCommentsMapper.listCommentsByBlogId(blogId);
        // 处理回复的用户信息
//        loadReply(comments);
        final List<CommentDTO> blogComments = processComments(comments);
        return blogComments;
    }

    private void loadReply(List<CommentDTO> comments) {
//        return comments.stream().filter(item -> !item.getAnswerId().equals(0)).map()
    }

    static List<CommentDTO> processComments(List<CommentDTO> list) {
        final List<CommentDTO> result = list.stream()
                .filter(comments -> comments.getParentId().equals(0L)) // 一级评论
                .map(item -> {
                    item.setComments(getComments(list, item));
                    return item;
                })
                .collect(Collectors.toList());
        return result;
    }

    private static List<CommentDTO> getComments(List<CommentDTO> list, BlogComments comments) {
        final List<CommentDTO> result = list.stream()
                .filter(item -> item.getParentId().equals(comments.getId()))
                .collect(Collectors.toList());
        return result;
    }
}




